package cn.wostore.wxhelper.ui.forward;


import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import butterknife.BindView;
import cn.wostore.wxhelper.R;
import cn.wostore.wxhelper.adapter.RuleListAdapter;
import cn.wostore.wxhelper.base.BaseActivity;
import cn.wostore.wxhelper.bean.ForwardRule;
import cn.wostore.wxhelper.event.WxDbSyncFinishEvent;
import cn.wostore.wxhelper.utils.DaoHandler;
import cn.wostore.wxhelper.utils.ToastUtil;
import cn.wostore.wxhelper.widget.CustomToolBar;
import cn.wostore.wxhelper.widget.LoadingDialog;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Fanghui at 2019-1-31
 */
public class RuleListActivity extends BaseActivity {

    private static final String TAG = RuleListActivity.class.getSimpleName();

    @BindView(R.id.tool_bar)
    CustomToolBar mCustomToolBar;

    @BindView(R.id.rv_rule_list)
    RecyclerView ruleListRv;

    private RuleListAdapter adapter;

    private List<ForwardRule> ruleList = new ArrayList<>();

    private LoadingDialog dialog = LoadingDialog.newInstance();


    @Override
    public int getLayoutId() {
        return R.layout.activity_rule_list;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView(Bundle savedInstanceState) {
        dialog.show(this.getFragmentManager(), TAG);
        initToolBar();
        initRecyclerView();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onWxDbSyncFinishEvent(WxDbSyncFinishEvent event) {
        dialog.dismiss();
        ToastUtil.showLong(mContext, getString(R.string.load_success_tips));
        getData();
    }

    private void initToolBar(){
        mCustomToolBar.setShowBack(false);
        mCustomToolBar.setRightActionItemResId(R.mipmap.ic_add);
        mCustomToolBar.setShowRightActionItem(true);
        mCustomToolBar.setShowTitle(true);
        mCustomToolBar.setTitle("转发列表");
        mCustomToolBar.setOnCustomToolBarListener(new CustomToolBar.OnCustomToolBarListener() {
            @Override
            public void onBackClick() {

            }

            @Override
            public void onRightActionItemClick() {
                Intent intent = new Intent(mContext, RuleSetActivity.class);
                startActivityForResult(intent, 99);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (requestCode == 99){
            getData();
        }
    }

    private void initRecyclerView() {
        adapter = new RuleListAdapter();
        adapter.setOnSwitchListener(new RuleListAdapter.OnSwitchListener() {
            @Override
            public void onSwitchOn(int id) {
                ContentValues contentValues = new ContentValues();
                contentValues.put("forwardRuleState", 1);
                DaoHandler.getInstance().update("ForwardRule", contentValues, id);
                getData();
            }

            @Override
            public void onSwitchOff(int id) {
                ContentValues contentValues = new ContentValues();
                contentValues.put("forwardRuleState", 0);
                DaoHandler.getInstance().update("ForwardRule", contentValues, id);
                getData();
            }
        });
        adapter.setmOnDeleteListener(new RuleListAdapter.OnDeleteListener() {
            @Override
            public void onDeleteClick(int id) {
                DaoHandler.getInstance().delete("ForwardRule", id);
                getData();
            }
        });
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        ruleListRv.setLayoutManager(layoutManager);
        ruleListRv.setAdapter(adapter);
    }

    private void getData() {
        Cursor cursor = DaoHandler.getInstance().query("ForwardRule", null, null, null, null);
        ruleList.clear();
        while (cursor.moveToNext()) {
            ForwardRule forwardRule = new ForwardRule();
            forwardRule.setValues(cursor);
            ruleList.add(forwardRule);
        }
        cursor.close();
        adapter.setData(ruleList);
    }
}
